<?php


namespace App\Http\Controllers\Weapp;


use App\Events\OrderIsPay;
use App\Jobs\OrderNotice;
use App\Model\OrderTransaction;
use App\Model\ReceiptCode;
use App\Service\Pay\PayFactory;
use App\Service\Pay\Weapp;
use App\Service\Pay\Wechat;
use App\Tool\NoticeType;
use App\Tool\PayType;
use App\Tool\Xml;
use Illuminate\Support\Facades\Log;

class Notify extends Base
{


    /**
     * 订单支付通知,微信
     * Create by Peter
     * 2019/12/23 11:08:25
     * Email:904801074@qq.com
     * @return bool|string
     * @throws \Exception
     */
    function checkOrderByWeChat()
    {


        try {
            \DB::beginTransaction();

            //获取支付方式
            $wechat = PayFactory::getPay('weapp');

            $res = $wechat->checkPay();

            if (!$res){

                \Log::debug(json_encode(['msg'=>'解码错误']));

                return false;
            }

            $attach = json_decode($res['attach'], true);

            $order_array = explode('*', $attach['id']);


            $transaction = OrderTransaction::create([
                'type' => $attach['type'],
                'total_fee' => $res['total_fee'] / 100,
                'pay_data' => json_encode($res),
                'transaction_id' => $res['transaction_id'],
                'order_id_array' => join(',', $order_array)
            ]);


            foreach ($order_array as $key => $value) {


                $info = \App\Model\Order::where('id', $value)
                    ->where('status', 1)
                    ->firstOrFail();


                $info->fill([
                    'status' => 11,
                    'order_transaction_id' => $transaction->id,
                    'pay_time' => time()
                ])->save();


                //自提时
                if($info->delivery_type==2) {

                    //生成提货码
                    ReceiptCode::createCode($info->id, $info->user_id);
                }

                //订单支付事件
                event(new OrderIsPay($info));



            }


//            \Log::debug(json_encode($res));

            \DB::commit();


            return $wechat->replySuccess();


        } catch (\Exception $exception) {

            \DB::rollBack();

            \Log::debug(($exception->getMessage()));

        }

    }


    /**
     * 退款通知,微信
     * Create by Peter
     * 2019/12/30 15:47:19
     * Email:904801074@qq.com
     */
    function refundNotifyByWeChat()
    {


        $wechat = PayFactory::getPay('weapp');


        $re = $wechat->refund_check();

        if (!$re) {

            \Log::debug('退款通知验证失败');

            return;

        }

        $out_refund_no = $re['out_refund_no'];

        $info = explode('_', $out_refund_no);

        $refund_id = $info[1];

        $pay_type = $info[2];


        $this->do_refund($refund_id);


//        \Log::debug(json_encode($re));


        return $wechat->replySuccess();

    }

    private function do_refund($refund_id)
    {


        $refund = \App\Model\Refund::where('id', $refund_id)->where(function ($query) {

            $query->where('status', 1)->orWhere('status', 2);
        })->first();


        $refund->fill(['status' => 3])->save();

    }


    /**
     * 获取通知数据
     * Create by Peter
     * 2019/12/30 16:18:30
     * Email:904801074@qq.com
     * @return false|mixed|string
     */
    protected function getData()
    {

        $php_version = PHP_MAJOR_VERSION;

        if ($php_version == 7) {

            $data = file_get_contents('php://input');

        } else {

            $data = $GLOBALS['HTTP_RAW_POST_DATA'];
        }


        return $data;

    }
}
